package com.wisezone.permission.service.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.wisezone.common.exception.WzDaoException;
import com.wisezone.common.exception.WzServiceException;
import com.wisezone.common.exception.WzWebException;
import com.wisezone.common.service.impl.BaseServiceImpl;
import com.wisezone.permission.bean.entity.WzGroupEntity;
import com.wisezone.permission.bean.entity.WzRescEntity;
import com.wisezone.permission.bean.entity.WzRescRoleEntity;
import com.wisezone.permission.bean.entity.WzRoleEntity;
import com.wisezone.permission.bean.search.WzRoleSearch;
import com.wisezone.permission.bean.search.WzUserRoleSearch;
import com.wisezone.permission.dao.WzRoleDao;
import com.wisezone.permission.service.WzRescRoleService;
import com.wisezone.permission.service.WzRescService;
import com.wisezone.permission.service.WzRoleService;

/**
 * 
 *角色; InnoDB free: 24576 kB
 * 
 * @author create by create date 2010-07-29 14:44:47 update date 2010-07-29
 *         14:44:47
 * 
 */
public class WzRoleServiceImpl extends BaseServiceImpl implements WzRoleService {

	/**
	  *
	  *
	  *
	  */
	private WzRoleDao wzRoleDao;

	private WzRescService wzRescService;

	private WzRescRoleService wzRescRoleService;
	
	/**
	 * 获得角色id和名称的map
	 * 
	 * @param id
	 * @return
	 */
	public Map<String, String> getMapByRoleId(Long id)
			throws WzServiceException {

		if (id == null) {
			throw new IllegalArgumentException("用户ID不能为空");
		}

		List<WzRoleEntity> list = this.wzRoleDao.listByUserId(id);

		if (list == null || list.size() == 0) {
			return null;
		}
		Map<String, String> map = new HashMap<String, String>();
		String ids = "";
		String names = "";

		for (Iterator<WzRoleEntity> it = list.iterator(); it.hasNext();) {
			WzRoleEntity entity = it.next();
			ids += "," + entity.getId();
			names += "," + entity.getName();
		}
		ids = ids.substring(1);
		names = names.substring(1);
		map.put("ids", ids);
		map.put("names", names);
		return map;

	}

	/**
	 * 获得激活的角色列表
	 * 
	 * @return
	 * @throws WzWebException
	 */
	public List<WzRoleEntity> listActive() throws WzServiceException {

		WzRoleSearch searchEntity = new WzRoleSearch();
		searchEntity.setIsActive(1); // 激活的
		List<WzRoleEntity> list = this.listAll(searchEntity);
		return list;
	}
	
	/**
	 * 获得角色列表
	 * @param userId
	 * @return
	 * @throws WzServiceException
	 */
	public List<WzRoleEntity> listRoleByUserId(Long userId) throws WzServiceException{
		
		List<WzRoleEntity> list = this.wzRoleDao.listByUserId(userId);
		return list;
	}
	
	/**
	 * 保存或修改角色
	 * 
	 * @param entity
	 * @param resIds
	 * @throws WzServiceException
	 */
	public void saveOrUpdate(WzRoleEntity entity, String resIds)
			throws WzServiceException {

		// 保存角色
		this.saveOrUpdate(entity);

		// 角色资源删除
		this.wzRescRoleService.deleteByRoleId(entity.getId());

		// 保存角色资源
		if (StringUtils.isNotEmpty(resIds)) {
			String resIdStr[] = resIds.split(",");
			for (String rescId : resIdStr) {

				WzRescRoleEntity rescRoleEntity = new WzRescRoleEntity();
				rescRoleEntity.setRescId(Long.valueOf(rescId));
				rescRoleEntity.setRoleId(entity.getId());
				wzRescRoleService.save(rescRoleEntity);
			}
		}
	}

	/**
	 * 角色删除
	 * 
	 * @param idList
	 * @throws WzServiceException
	 */
	public void delete(Collection<Serializable> idList)
			throws WzServiceException {

		// 空就返回
		if (idList == null || idList.size() == 0) {
			return;
		}

		// 删除资源角色
		for (Iterator<Serializable> it = idList.iterator(); it.hasNext();) {
			Long id = Long.valueOf(it.next().toString());
			this.wzRescRoleService.deleteByRoleId(id);
		}

		// 删除角色
		this.deleteAll(idList);
	}

	public void setWzRoleDao(WzRoleDao wzRoleDao) {
		this.setBaseDao(wzRoleDao);
		this.wzRoleDao = wzRoleDao;
	}

	public void setWzRescService(WzRescService wzRescService) {
		this.wzRescService = wzRescService;
	}

	public void setWzRescRoleService(WzRescRoleService wzRescRoleService) {
		this.wzRescRoleService = wzRescRoleService;
	}

}